<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的订单</title>
    <link href="../asserts/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
    <script src="../asserts/plugins/jquery-3.6.0.min.js"></script>
    <script src="../asserts/plugins/bootstrap/js/bootstrap.js"></script>

    <script>
        $(function () {
            // 展示我的订单（我买的）
            function getOrderList_buy() {
                $.ajax({
                    url:"/KunKunMarket/OrderController",
                    type:"get",
                    dataType:"json",
                    data:{
                        type: 'getOrder_0'
                    },
                    success:function (respBean) {
                        if (respBean.status == 500){
                            console.log(respBean.msg);
                            return;
                        }
                        $("#myOrderList_buy").text('');
                        for (let i=0; i<respBean.data.length; i++){
                            let $tr = $(`
                            <tr class="buy_tr">
                                <td>
                                    <span class="order_id"></span>
                                    <span class="user_id_0" style="display: none"></span>
                                    <span class="user_id_1" style="display: none"></span>
                                </td>
                                <td class="datetime">2020 11 29 </td>
                                <td class="commodity_id">17</td>
                                <td class="address">广东省白云市</td>
                                <td class="phone">19711111111</td>
                                <td class="status" style="color: red">未发货</td>
                                <td class="edit"># # # # </td>
                            </tr>
                        `);
                            $tr.find(".order_id").text(respBean.data[i].id);
                            $tr.find(".user_id_0").text(respBean.data[i].user_id_0);
                            $tr.find(".user_id_1").text(respBean.data[i].user_id_1);
                            $tr.find(".address").text(respBean.data[i].address)
                            $tr.find(".commodity_id").text(respBean.data[i].commodity_id);

                            let date = new Date(respBean.data[i].datetime);
                            $tr.find(".datetime").text(date);

                            $tr.find(".phone").text(respBean.data[i].phone)

                            let statusStr = '未发货';
                            if (respBean.data[i].status == 1){
                                statusStr = '已发货';
                                $tr.find(".status").attr("style","color: blue");
                            }else if (respBean.data[i].status == 2){
                                $tr.find(".status").attr("style","color: ");
                                statusStr = '已完成';
                            }
                            $tr.find(".status").text(statusStr);

                            $('#myOrderList_buy').append($tr);
                            if (respBean.data[i].status == 1){
                                let $shouHuo_btn = (`<button class="shouHuo_btn">确认收货</button>`)
                                $tr.find(".edit").html($shouHuo_btn);
                            }
                            //添加确认收货按钮请求功能
                            $(".shouHuo_btn").click(shouHuo_btn);
                        }

                    }
                })
            }
            getOrderList_buy();
            //确认收货，发送请求到后端
            function shouHuo_btn(){

                //获取到订单号id，商品id，买家id，卖家id
                let order_id = $(this).parents(".buy_tr").find(".order_id").text();
                let commodity_id = $(this).parents(".buy_tr").find(".commodity_id").text();
                let user_id_0 = $(this).parents(".buy_tr").find(".user_id_0").text();
                let user_id_1 = $(this).parents(".buy_tr").find(".user_id_1").text();
                $.ajax({
                    url: "/KunKunMarket/OrderController",
                    type: "get",
                    dataType:"json",
                    data: {
                        type: "Receipt",
                        order_id: order_id,
                        commodity_id: commodity_id,
                        user_id_0: user_id_0,
                        user_id_1: user_id_1
                    },
                    success:function (respBean) {
                         if (respBean.status == 200){
                             getOrderList_buy();
                         }
                    }
                })
            }


            // 展示我的订单（我卖出的）
            function getOrderList_sell() {
                $.ajax({
                    url:"/KunKunMarket/OrderController",
                    type:"get",
                    dataType:"json",
                    data:{
                        type: 'getOrder_1'
                    },
                    success:function (respBean) {
                        if (respBean.status == 500){
                            console.log(respBean.msg);
                            return;
                        }
                        $("#myOrderList_sell").text('');
                        for (let i=0; i<respBean.data.length; i++){
                            let $tr = $(`
                            <tr class="buy_tr">
                                <td>
                                    <span class="order_id"></span>
                                    <span class="user_id_0" style="display: none"></span>
                                    <span class="user_id_1" style="display: none"></span>
                                </td>
                                <td class="datetime">2020 11 29 </td>
                                <td class="commodity_id">17</td>
                                <td class="address">广东省白云市</td>
                                <td class="phone">19711111111</td>
                                <td class="status" style="color: red">未发货</td>
                                <td class="edit"># # # # </td>
                            </tr>
                        `);
                            $tr.find(".order_id").text(respBean.data[i].id);
                            $tr.find(".user_id_0").text(respBean.data[i].user_id_0);
                            $tr.find(".user_id_1").text(respBean.data[i].user_id_1);
                            $tr.find(".address").text(respBean.data[i].address)
                            $tr.find(".commodity_id").text(respBean.data[i].commodity_id);

                            let date = new Date(respBean.data[i].datetime);
                            $tr.find(".datetime").text(date);

                            $tr.find(".phone").text(respBean.data[i].phone)

                            let statusStr = '未发货';
                            if (respBean.data[i].status == 1){
                                statusStr = '已发货';
                                $tr.find(".status").attr("style","color: blue");
                            }else if (respBean.data[i].status == 2){
                                $tr.find(".status").attr("style","color: ");
                                statusStr = '已完成';
                            }
                            $tr.find(".status").text(statusStr);

                            console.log(respBean.data[i]);
                            $('#myOrderList_sell').append($tr);
                            if (respBean.data[i].status == 0){
                                let $faHuo_btn = (`<button class="faHuo_btn">点击发货</button>`)
                                $tr.find(".edit").html($faHuo_btn);
                            }

                        }
                        //添加发货按钮请求功能
                        $(".faHuo_btn").click(faHuo_btn);
                    }
                })
            }
            getOrderList_sell();
            //发货，请求到后端
            function faHuo_btn() {

                //获取到订单号id，商品id，买家id，卖家id
                let order_id = $(this).parents(".buy_tr").find(".order_id").text();
                $.ajax({
                    url: "/KunKunMarket/OrderController",
                    type: "get",
                    dataType:"json",
                    data: {
                        type: "Shipment",
                        order_id: order_id
                    },
                    success:function (respBean) {
                        if (respBean.status == 200){
                            getOrderList_sell();
                        }
                    }
                })
            }
        })
    </script>
</head>
<body>
    <a href="/KunKunMarket/UserController?type=logout">退出登录</a>

    <h1 style="text-align: center">我的订单</h1>

    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <h2>我购买的：</h2>
            </div>
        </div>
        <table class="table table-striped">
            <thead>
            <tr>
                <!--
                    买家和卖家id隐藏在第一列中
                    user_id_0: 2
                    user_id_1: 2
                -->
                <th>订单号</th>
                <th>创建时间</th>
                <th>商品id</th>
                <th>收货地址</th>
                <th>收货电话</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="myOrderList_buy">
                <tr>
                    <td colspan="7" style="text-align: center">未获取到任何数据</td>
                </tr>
            </tbody>
        </table>


        <!--我卖出的-->
        <div class="row">
            <div class="col-md-12">
                <h2>我卖出的：</h2>
            </div>
        </div>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>订单号</th>
                <th>创建时间</th>
                <th>商品id</th>
                <th>收货地址</th>
                <th>收货电话</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="myOrderList_sell">
                <tr>
                    <td colspan="7" style="text-align: center">未获取到任何数据</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
</html>